import router from '@/router'
import store from '@/store'
// 进度条方法和样式导入
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 路由守卫进行权限控制
// 白名单
const whiteList = ['/login', '/404']
router.beforeEach((to, from, next) => {
  // 开启进度条
  NProgress.start()
  console.log('去哪儿', to.path)
  console.log('从哪儿来', from.path)
  // to and from are both route objects. must call `next`.
  const token = store.state.user.token
  if (token) {
    // 已登录
    if (to.path === '/login') {
      console.log('你已登录,直接去首页')
      next('/')
      NProgress.done()// 关闭进度条
    } else {
      console.log('已经登录,放行')
      next()
    }
  } else {
    // 未登录
    if (whiteList.includes(to.path)) {
      console.log('拥有白名单,可以直接访问')
      next()
    } else {
      console.log('没有白名单,请先登录')
      next('/login') // 关闭进度条
    }
  }
})

router.afterEach(() => {
  console.log('页面跳转完成会经过这里')
  NProgress.done()// 关闭进度条
})
